package com.glsc.ngateway.opmanage.service.deposit;

import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.deposit.DepositoryDayEndTrialCcb;
import com.glsc.ngateway.common.base.repo.mysql.opmanagemain.deposit.DepositoryDayEndTrialCcbRepository;
import com.glsc.ngateway.opmanage.utils.DateUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.List;
import java.util.Objects;

@Service
public class DepositoryDayEndTrialCcbService {
    @Resource
    private DepositoryDayEndTrialCcbRepository depositoryDayEndTrialCcbRepository;


    /**
     * 保存
     *
     * @param obj
     */
    @Transactional
    public DepositoryDayEndTrialCcb save(DepositoryDayEndTrialCcb obj) {

        DepositoryDayEndTrialCcb oldData = findByNameAndDate(obj.getBankName(), obj.getEffectDate());
        if (Objects.nonNull(oldData)) {
            depositoryDayEndTrialCcbRepository.deleteById(oldData.getId());
        }
        obj.setDeleted("N");
        return depositoryDayEndTrialCcbRepository.save(obj);
    }

    /**
     * @param name
     * @param date
     * @return
     */
    public DepositoryDayEndTrialCcb findByNameAndDate(String name, String date) {
        List<DepositoryDayEndTrialCcb> list = depositoryDayEndTrialCcbRepository.findAll(Example.of(DepositoryDayEndTrialCcb.builder().bankName(name).deleted("N").effectDate(date).build()));
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list.get(0);
    }


}
